<?php
Yii::import('ext.validators.PhoneValidator');

class ContactForm extends CFormModel {
    
    public $full_name, $address, $email, $phone, $content;
    public $file, $fileUploadedInst;
    public $verify_code;
    public $isEntering = false; //have least one input entered

    public function rules() {
        return array(
            array('full_name, address, email, phone, content',
                'required',
                'message' => Yii::t('msg', 'please_input')
            ),
            array('verify_code',
                'required',
                'message' => Yii::t('msg', 'verifycode_not_match')
            ),
            array('email',
                'email',
                'message' => Yii::t('msg', 'incorret_format')
            ),
            array('phone',
                'PhoneValidator',
                'message' => Yii::t('msg', 'incorret_format'),
            ),
            array('verify_code', 'captcha',
                'allowEmpty' => false,
            ),
            array('email',
                'length', 'max' => 255),
            array('phone', 'length', 'max' => 32),
            array('file', 'validateFile'),
            array('file, fileUploadedInst', 'safe'),
        );
    }

    public function attributeLabels() {
        return array(
            'full_name'   => 'Họ và tên',
            'address'     => 'Địa chỉ',
            'email'       => 'Email',
            'phone'       => 'Điện thoại',
            'content'     => 'Nội dung',
            'verify_code' => 'Gõ mã xác thực bên cạnh',
        );
    }

    /* ------------------------------------------------------------------------
      SEND MAIL
    -------------------------------------------------------------------------*/
    /**
     * Render attribute values to HTML format.
     * Support to send mail.
     * @return string
     */
    public function renderHtml() {
        $tplPath = Yii::app()->params['mail_template_dir'] . 'contact.txt';
        $result = null;
        
        if (is_file($tplPath)) {
            $content = file_get_contents($tplPath);
            $result = sprintf($content,
                $this->full_name,
                $this->address,
                $this->email,
                $this->phone,
                nl2br($this->content)
            );
        }
        
        return $result;
    }
    /* ------------------------------------------------------------------------
      //SEND MAIL
    -------------------------------------------------------------------------*/
    
    /**
     * ============================ PROCESSING FILE ============================
     */
    public function validateFile($attribute, $params) {
        $file = CUploadedFile::getInstance($this, $attribute);
        $uploadSuccess = true;
        
        if ($file) {//file is uploaded
            $aValid = array('zip', 'rar', 'pdf', 'doc', 'docx', 'xls', 'xlsx');
            
            if ( ! in_array($file->getExtensionName(), $aValid)) {
                $this->addError($attribute, Yii::t('msg', 'contact_file_format'));
                $uploadSuccess = false;
            }
            if ( $uploadSuccess && ($file->getSize() > 10000) ) {
                $this->addError($attribute, Yii::t('msg', 'contact_file_size'));
                $uploadSuccess = false;
            }
            
            if ($uploadSuccess) {
                $this->fileUploadedInst = $file;
            }
        }
        
        return TRUE;
    }
    /**
     * ============================ END OF PROCESS FILE ============================
     */
}
